<?php

namespace App\Console\Commands;

use App\Services\UploadService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

/**
 * 图片上传阿里云oss
 */
class UploadPictureToAliOss extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:uploadPicture';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->uploadOss();
        return 0;
    }


    /**
     * 上传oss
     */
    public function uploadOss()
    {
        return false;

        $uploadService = new UploadService();

        $id = 412959;

        while(true)
        {
            $data = DB::table('warranty_image')->select("id", "image_src")
                ->where('id',  '>', $id)
                ->where('id', '<=', 451427)
                ->limit(1000)
                ->get();

            if(empty($data))
            {
                break;
            }

            foreach($data as $item)
            {
                if(file_exists(public_path() . $item->image_src))
                {
                    $fileName = str_replace('/upload/', '', $item->image_src);
                    $imageUrl = $uploadService->uploadAliOss(file_get_contents(public_path() . $item->image_src), $fileName);

                    if(empty($imageUrl['oss-request-url']))
                    {
                        continue;
                    }

                    $img = str_replace('https://xpel-dengwho.oss-cn-shanghai.aliyuncs.com', 'https://image-xpel.dengwho.com', $imageUrl['oss-request-url']);

                    DB::table('warranty_image')->where('id', $item->id)->update(["image_src" => $img]);
                }
            }

            $id += 1000;
        }
    }
}
